
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public abstract class PlayListView extends Canvas {

    public Playlist[] playlist;
    public String title;
    private int selectedIndex;
    private int lrtb, height, top;
    private int curView, rows;
    private Image icon, bgImage;

    public PlayListView() {
        selectedIndex = 0;
        title = "Playlist";
        curView = 0;
        lrtb = 5;
        top = 30;
        height = 45;
        rows = (this.getHeight() - top - lrtb) / height;
        try {
            icon = Image.createImage("/playlist.png");
            bgImage = Image.createImage("/background.jpg");
        } catch (Exception ex) {
        }
    }

    public void loadItems(Playlist[] playlists) {
        this.playlist = new Playlist[playlists.length];
        for (int i = 0; i < playlist.length; i++) {
            this.playlist[i] = playlists[i];
        }
        selectedIndex = 0;
        curView = 0;
        repaint();
    }

    public void setImage(String url) {
        try {
            icon = Image.createImage(url);
        } catch (Exception ex) {}
    }

    protected void paint(Graphics g) {
        if (selectedIndex >= (curView + rows)) {
            curView = selectedIndex - rows + 1;
        } else if (selectedIndex < curView) {
            curView = selectedIndex;
        }
        Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
        Font f1 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC | Font.STYLE_PLAIN, Font.SIZE_LARGE);
        g.setFont(f);
        g.drawImage(bgImage, 0, 0, Graphics.TOP | Graphics.LEFT);
        g.setColor(0xEFEFEF);
        g.drawString(title, this.getWidth() / 2, top / 2, Graphics.BASELINE | Graphics.HCENTER);
        if (playlist.length > 0) {
            for (int i = 0; i < rows; i++) {
                g.setColor(0xEFEFEF);
                if (i+curView == selectedIndex){
                    g.fillRect(lrtb, top + lrtb + i * height, this.getWidth() - 2 * lrtb, height);
                    g.setColor(0x000000);
                }
                g.drawRegion(icon, 0, 0, 36, 36, Sprite.TRANS_NONE, lrtb + height / 2, top + lrtb + i * height + height / 2, Graphics.VCENTER | Graphics.HCENTER);
                g.setFont(f);
                g.drawString(playlist[curView + i].name, lrtb * 4 + height, top + lrtb + i * height + height / 4, Graphics.LEFT | Graphics.BASELINE);
                g.setFont(f1);
                g.drawString(playlist[curView + i].artis, lrtb * 4 + height, top + lrtb + i * height + (height * 3) / 4, Graphics.LEFT | Graphics.BASELINE);
                if (i + 1 == this.playlist.length) {
                    break;
                }
            }
        } else {
            g.drawString("<Khong co>", this.getWidth() / 2, this.getHeight() / 2, Graphics.BASELINE | Graphics.HCENTER);
        }
    }

    protected void keyPressed(int keyCode) {
        if (getKeyName(keyCode).equals("Down")) {
            selectedIndex++;
            if (selectedIndex >= playlist.length) {
                selectedIndex = 0;
            }
            repaint();
        }
        if (getKeyName(keyCode).equals("Up")) {
            selectedIndex--;
            if (selectedIndex < 0) {
                selectedIndex = playlist.length - 1;
            }
            repaint();
        }
        if (getKeyName(keyCode).equals("Right") || getKeyName(keyCode).equals("Left")) {
            onLeftRight();
        }
        repaint();
        if (getKeyName(keyCode).equals("Select")) {
            onSelect(selectedIndex);
        }
    }

    public abstract void onSelect(int selectedIndex);

    public abstract void onLeftRight();
}